package com.huangwenshuo.futurestarschoolmanagement.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huangwenshuo.futurestarschoolmanagement.mapper.ClassesMapper;
import com.huangwenshuo.futurestarschoolmanagement.mapper.PositionMapper;
import com.huangwenshuo.futurestarschoolmanagement.mapper.StudentsMapper;
import com.huangwenshuo.futurestarschoolmanagement.pojo.dto.Page;
import com.huangwenshuo.futurestarschoolmanagement.pojo.po.Classes;
import com.huangwenshuo.futurestarschoolmanagement.pojo.po.Position;
import com.huangwenshuo.futurestarschoolmanagement.pojo.po.Students;
import com.huangwenshuo.futurestarschoolmanagement.pojo.vo.StudentVo;
import com.huangwenshuo.futurestarschoolmanagement.service.StudentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huangwenshuo.futurestarschoolmanagement.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huangwenshuo
 * @since 2020-12-24
 */
@Service
public class StudentsServiceImpl extends ServiceImpl<StudentsMapper, Students> implements StudentsService {

    @Autowired
    private StudentsMapper mapper;

    @Autowired
    private PositionMapper positionMapper;

    @Autowired
    private ClassesMapper classesMapper;

    @Override
    public List<StudentVo> getStuPages(Page page) {
        List<StudentVo> studentVos = mapper.getStuPages(new Page(page));
        return studentVos;
    }

    @Override
    public Integer getStuTotal(Page page) {
        Integer total = mapper.getStuTotal(new Page(page));
        return total;
    }

    @Override
    public List<String> getClassNames(Page page) {

        return mapper.getClassNames(page);
    }

    @Override
    public List<String> getPositionNames(Page page) {
        return mapper.getPositionNames(page);
    }

    @Override
    public void updateStu(StudentVo studentVo) {
        QueryWrapper q1 = new QueryWrapper<>().eq("class_name",studentVo.getClassName());
        Classes classes = classesMapper.selectOne(q1);
        if(classes == null){
            Classes c = new Classes();
            c.setClassName(studentVo.getClassName());
             classesMapper.insert(c);
        }
        QueryWrapper q2 = new QueryWrapper<>().eq("position_name",studentVo.getPositionName());
        Position position = positionMapper.selectOne(q2);
        if(position == null){
            Position p = new Position();
            p.setPositionName(studentVo.getPositionName());
            positionMapper.insert(p);
        }
        classes = classesMapper.selectOne(q1);
        position = positionMapper.selectOne(q2);
        mapper.updateStu(studentVo.getUid(),classes.getClassId(),position.getPositionId());

    }

    @Override
    public void deleteStu(String uid) {
        mapper.deleteStu(uid);
    }

    @Override
    public void addStu(StudentVo studentVo) {
        QueryWrapper q1 = new QueryWrapper<>().eq("class_name",studentVo.getClassName());
        QueryWrapper q2 = new QueryWrapper<>().eq("position_name",studentVo.getPositionName()!=null?studentVo.getPositionName():"学生");
        Classes classes = classesMapper.selectOne(q1);
        Position position = positionMapper.selectOne(q2);
        Map<String,Object> student = null;
        List<Integer> students = new ArrayList<>();
        for(Integer uid : studentVo.getUids()){
             student = mapper.SelectStu(uid,classes.getClassId());
             students.add(uid);
        }
        if(student != null){
            throw new RuntimeException("uid为"+ Arrays.toString(students.toArray()) +"的学生已经存在");
        }
        for(Integer uid : studentVo.getUids()){
            mapper.addStu(uid,classes.getClassId(),position.getPositionId());
            students.add(uid);
        }

    }

    @Override
    public void exportStu(Page page, HttpServletResponse response) {
        List<StudentVo> list = mapper.exportStu(page);
        ExcelUtils<StudentVo> util = new ExcelUtils<>(StudentVo.class);
        util.exportExcel(list, response, "stus");
    }
}
